<template>
  <div class="mod-config">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item>
        <el-input v-model="dataForm.key" placeholder="参数名" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
<!--        <el-button v-if="isAuth('map:maptable:save')" type="primary" @click="addOrUpdateHandle()">新增</el-button>-->
<!--        <el-button v-if="isAuth('map:maptable:delete')" type="danger" @click="deleteHandle()" :disabled="dataListSelections.length <= 0">批量删除</el-button>-->
      </el-form-item>
    </el-form>
    <el-row :gutter="24">
      <el-col :span="6" v-for="(item, index) in this.dataList" :key="index">
        <el-card class="map-card" :body-style="{ padding: '0px' }">
          <img :src="item.mapLocalpath" class="map-image">
          <div style="padding: 14px;">
            <span>{{item.mapName}}</span>
            <div class="bottom clearfix">
              <el-button type="text" class="button"
                         :type="item.mapIsload ? 'primary' : 'danger'"
                         @click="isUsed(item)">状态：{{ item.mapIsload ? '已启用' : '未启用' }}</el-button>
            </div>
          </div>
        </el-card>
      </el-col>
    </el-row>
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
  </div>
</template>

<script>
import AddOrUpdate from './maptable-add-or-update'
export default {
  data () {
    return {
      dataForm: {
        key: ''
      },
      selectedMapIndex: -1, // 默认没有选中任何地图项，-1 表示没有选中
      currentDate: new Date(),
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      dataListSelections: [],
      addOrUpdateVisible: false,
      radio: '',
      choose: '',
      selectedMapPath: '' // 添加一个变量来存储当前选中地图的路径
    }
  },
  components: {
    AddOrUpdate
  },
  activated () {
    this.getDataList()
  },
  methods: {
    isUsed(item){
      console.log(item.mapNo)
      this.$confirm('确定吗?','提示',{
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$http({
          url: this.$http.adornUrl('/map/maptable/updatestatus'),
          method: 'get',
          params: this.$http.adornParams({
            'mapNo': item.mapNo
          })
        }).then(({data}) => {
          if (data) {
            this.$message({
              type: 'success',
              message: '成功!',
              duration: 1500,
              onClose: () => {
                this.getDataList()
              }
            })
          } else {
            this.dataList = []
          }
        })
      }).catch(() => {
        this.$message({

        });
      })
      },
    handleCurrentChange(val) {
      this.currentRow = val;
    },
    tableRowClassName({row, rowIndex}) {
      if ((rowIndex + 1) % 2 === 0) {
        return 'success-row';
      }
      return '';
    },
    // 获取数据列表
    getDataList () {
      this.dataListLoading = true
      this.$http({
        url: this.$http.adornUrl('/map/maptable/list'),
        method: 'get',
        params: this.$http.adornParams({
          'page': this.pageIndex,
          'limit': this.pageSize,
          'key': this.dataForm.key
        })
      }).then(({data}) => {
        if (data && data.code === 0) {
          this.dataList = data.page.list
          this.totalPage = data.page.totalCount
          // 筛选已启用的地图并提取其mapLocalpath到一个数组中
          const enabledMaps = this.dataList.filter(item => item.mapIsload === 1);
          const mapLocalpaths = enabledMaps.map(item => item.mapLocalpath);

          console.log(mapLocalpaths)
          // 存储所有已启用地图的mapLocalpath到localStorage中
          localStorage.setItem('enabledMapLocalpaths', JSON.stringify(mapLocalpaths));
        } else {
          this.dataList = []
          this.totalPage = 0
        }
        this.dataListLoading = false
      })
    },
    // 每页数
    sizeChangeHandle (val) {
      this.pageSize = val
      this.pageIndex = 1
      this.getDataList()
    },
    // 当前页
    currentChangeHandle (val) {
      this.pageIndex = val
      this.getDataList()
    },
    // 多选
    selectionChangeHandle (val) {
      this.dataListSelections = val
    },
    // 新增 / 修改
    addOrUpdateHandle (id) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id)
      })
    },
    // 删除
    deleteHandle (id) {
      var ids = id ? [id] : this.dataListSelections.map(item => {
        return item.mapNo
      })
      this.$confirm(`确定对[id=${ids.join(',')}]进行[${id ? '删除' : '批量删除'}]操作?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$http({
          url: this.$http.adornUrl('/map/maptable/delete'),
          method: 'post',
          data: this.$http.adornData(ids, false)
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.getDataList()
              }
            })
          } else {
            this.$message.error(data.msg)
          }
        })
      })
    }
  }
}
</script>
<style lang="scss">
.map-card {
  width: 100%;
  height: 200px; /* Set the desired height */
}

.map-image {
  width: 100%;
  height: 120px; /* Set the desired height */
  object-fit: cover; /* Preserve the aspect ratio and cover the container */
}
.time {
  font-size: 13px;
  color: #999;
}

.bottom {
  margin-top: 13px;
  line-height: 12px;
}

.button {
  padding: 0;
  float: right;
}

.image {
  width: 100%;
  display: block;
}

.clearfix:before,
.clearfix:after {
  display: table;
  content: "";
}

.clearfix:after {
  clear: both
}
.tr-color-0 {
  background: #f0f0f0;
}
.tr-color-1 {
  background: #fff;
}
.el-table .success-row {
  background: #DFEAF5;
}
</style>
